/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/

package lime2;
import java.util.*;

/**
 * Models a registered reaction. RegisteredReaction instances are not directly manipulated by application programmers;
 * they are managed by Lime internals. Each registered reaction has a ReactionID (unique wrt the whole "federated" 
 * system) and stores the TupleIDs of all the tuples it has reacted to (useful for once-per-tuple reactions).
 * Please note that since the "equals" method has not been (re)defined, RegisteredReaction A equals RegisteredReaction
 * B only if they are the same reference (==). 
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

class RegisteredReaction extends Reaction
{
 // the hashtable[TupleID,TupleID] of tuples that have been reacted to by this registered reaction
 private Hashtable tuplesReacted;   

 // the unique id of this registered reaction
 private ReactionID rid;
 
 /**
  * Constructor method. It is package visible since the application programmer cannot directly instantiate a 
  * registered reaction.
  * 
  * @param r a Reaction 
  * @param rid a valid ReactionID 
  */
 RegisteredReaction(Reaction r, ReactionID rid)
 {
  super(r.getMode(), r.getType(), r.getTemplate(), r.getListener()); 
  tuplesReacted = new Hashtable(50);	
  this.rid = rid;
 }
 
 /**
  * Determines whether the tuple represented by the passed TupleID still needs to be reacted to or not. If the tuple 
  * has not been reacted to, then a side-effect of "alreadyReactedTo" is that the TupleID is put into the private data
  * structure of the tuples alredy reacted to. 
  * 
  * @param ltid the TupleID to determine whether it has alredy been reacted to
  * @return boolean
  */
 boolean alreadyReactedTo(TupleID ltid) 
 {
  boolean ret = false;
  if (tuplesReacted.containsKey(ltid)) ret = true;
  else tuplesReacted.put(ltid, ltid);
  return ret;
 }
 
 /**
  * Returns the identifier of this registered reaction.
  * 
  * @return identifier of the registered reaction
  */
 ReactionID getReactionID()
 {
  return rid;	
 }
 
}
